// created by jimmy 2016/2/24

var loginCallBack;
/*为关闭按钮绑定点击事件*/
$(".btn-close").click(closeLoginDiglog);
/*关闭登录窗口*/
function closeLoginDiglog() {
    loginCallBack = null;
    /*移除 active : opacity从 1 到 0*/
    $(".login-bg").removeClass("active");
    $(".login-box").removeClass("active");
    /*延时 300毫秒 */
    setTimeout(function() {
        /*登录页面添加 hide : 隐藏登录页面*/
        $(".index-login").addClass("hide");
    }, 300);
}

/*打开登录窗口*/
function openLoginDiglog(callback) {
    if (callback != null)
        loginCallBack = callback;

    $(".index-login").removeClass("hide");
    $(".alert-box").addClass("hide");
    $(".loading-box").addClass("hide");
    $(".login-box").removeClass("hide");
    setTimeout(function() {
        $(".login-bg").addClass("active");
        $(".login-box").addClass("active");
    }, 10);

    createCaptcha($(".get-pic-code"));
    if ($(".login-dialog").find(".phoneInput").val().length == 11) {
        // checkPhoneReq($(".login-dialog").find(".phoneInput"), checkPhoneReqCallback);
        $(".login-dialog").find(".phoneInput").attr("data-flag", "1");
        var btnGetCode = $(".login-dialog").find(".phoneInput").parent().siblings(".code").find("button");
        btnGetCode.css("background-color", "#4f6dcf");
        btnGetCode.css("cursor", "pointer");
    }

    checkLoginBtn($(".login-dialog").find(".phoneInput"));
}

function setLoginCallBack(callback) {
    if (callback != null)
        loginCallBack = callback;
    else
        loginCallBack = null;
}

var reqCallBack = function() {

        $(".phoneInput").val("");
        $(".codeInput").val("");
        $(".pswInput").val("");

        if (loginCallBack != null)
            loginCallBack();

        loginCallBack = null;

        closeLoginDiglog();
    }
    /*为‘登录’ 按钮 绑定点击事件*/
$(".btn-login").click(function() {
    var fa = $(this).parent();
    if (!checkPhone(fa.find(".phoneInput")))
        return;
    if (type == 0) {
        if (!checkPassword(fa.find(".pswInput"))) {
            return;
        }
        if (!checkCode(fa.find(".codePicInput"))) {
            return;
        }
        userLogin(fa.find(".phoneInput").prop("value"), fa.find(".pswInput").prop("value"), fa.find(".codePicInput").prop("value"), reqCallBack);
    } else {
        if (!checkCode(fa.find(".codeInput")))
            return;
        quickLogin(fa.find(".phoneInput").prop("value"), fa.find(".codePicInput").prop("value"), fa.find(".codeInput").prop("value"), reqCallBack);
    }
});

/*账号登录接口*/
function userLogin(phone, password, code, loginCallback) {
    var object = {
        phone: phone,
        password: password,
        code: code
    };
    var action = "userLogin";
    login(object, action, loginCallback);
}
/*免注册登录接口*/
function quickLogin(phone, code, smsCode, loginCallback) {
    var object = {
        phone: phone,
        code: code,
        sms_code: smsCode
    };
    var action = "quickLogin";
    login(object, action, loginCallback);
}

function login(object, action, finalloginCallback) {
    $.ajax({
        type: "get",
        async: true, //异步请求
        url: '/user/' + action + '?rondom=' + Math.random(),
        data: object,
        timeout: 3000,
        success: function(datas) {

            if (datas.status == "0000") {

                if ($(".radio-input").prop("checked")) {
                    setCookie("check-phone", $(".login-dialog").find(".phoneInput").prop("value"), 1, 1, null);
                } else {
                    deleteCookie("check-phone");
                }

                $(".user-name").attr("data-user_id", datas.user_id);
                $(".user-name").text(datas.nick_name);
                $(".mine-login").removeClass("hide");
                $(".mine-logout").addClass("hide");

                requestMessageList();

                if (finalloginCallback != null)
                    finalloginCallback();

            } else if (datas.status == "0201") {

                showErrorTipsByAn($(".login-dialog").find(".errorPhoneCodeTips"), "账号或者密码错误");
            } else if (datas.status == "0202") {
                if (type == 0) {
                    showErrorTips($(".login-dialog").find(".errorPicCodeTips"), datas.msg);
                } else {
                    showErrorTips($(".login-dialog").find(".errorCodeTips"), datas.msg);
                }
            } else if (datas.status == "1011") {
                showErrorTips($(".login-dialog").find(".errorPicCodeTips"), datas.msg);
            } else if (datas.status == "0223") {
                showErrorTipsByAn($(".login-dialog").find(".errorPhoneCodeTips"), "账号或者密码错误");
            } else if (datas.status == "0208") {
                showErrorTipsByAn($(".login-dialog").find(".errorPhoneCodeTips"), "账号或者密码错误");
            } else if (datas.status == "4001") { //防刷限制
                showErrorTipsByAn($(".login-dialog").find(".errorPhoneCodeTips"), datas.msg);
            }
        },
        error: function() {
            $.nmDebug.log(action, "失败，请稍后再试！");
        }
    });
}

$(document).ready(function() {

    phoneInputListener($(".login-dialog").find(".phoneInput"));
    codeInputListener($(".login-dialog").find(".codeInput"));
    passwordInputListener($(".login-dialog").find(".pswInput"));
});
